const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    inputValue:'',
    page:1,
    pageSize:10,
    isEnd:false,
    contentlistdata:[],
    categorylistdata:[],
    categoryId:'',
    mallindex:0,
    isloading:true
  },
  onLoad: function (options) {
    // if(optionss.search){
    //   this.setData({
    //       inputValue: optionss.search || ''
    //   })
    // }
    this.categorylist()
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }

    if(options.title){
      wx.setNavigationBarTitle({
        title: options.title
      })
    }
  },
  changemall(e){
    var index=e.currentTarget.dataset.index;
    var title=e.currentTarget.dataset.title || '';
    var id=e.currentTarget.dataset.id || '';
    console.log(e.currentTarget.dataset)
    this.setData({
      page:1,
      pageSize:10,
      isEnd:false,
      contentlistdata:[],
      mallindex:index,
      categoryId:id,
      dataType:e.currentTarget.dataset.datatype
    })
    this.contentlist()
  },
  golink3(e){
    var item=e.currentTarget.dataset.item;
    console.log(item)
    if(this.data.dataType == 2){

      if(item.lessonCount >= 2){
        wx.navigateTo({
          url:"/pages/expert/expert?id="+item.id+'&is=true'
        });
      }else{
        wx.navigateTo({
          url:"/pages/videoDetails/videoDetails?id="+item.defaultContentId
        });
      }
      
      
      return;
    }

    if(item.type==1){
      wx.navigateTo({
        url:"/pages/news/detail?id="+item.id
      });
    }else if(item.type==2 || item.type==3){
      if(item.lessonId){
        wx.navigateTo({
          url:"/pages/videoDetails/videoDetails?id="+item.id
        });
      }else{
        wx.navigateTo({
          url:"/pages/news/detail?id="+item.id
        });
      }
    }
  },
  categorylist(){
    Controller.categorylist({source:1}).then(res=>{
      if(res.data.baseData.length>0){
        this.setData({
          categorylistdata:res.data.baseData,
          categoryId:res.data.baseData[0].id,
          dataType:res.data.baseData[0].dataType,
        })
        // console.log(this.data.categorylistdata)
        this.contentlist()
      }
    })
  },
  contentlist(){
    var data={
      keyword:this.data.inputValue,
      source:1,
      orderBy:1,
      page:this.data.page,
      pageSize:this.data.pageSize,
      categoryId:this.data.categoryId,
      lessonId:"",
      isFollow:"",
      isRecommend:1,
      isShow:1,
    }
    if(this.data.dataType == 1){
      Controller.contentlist(data).then(res=>{
        if(res.data.baseData && res.data.baseData.list.length>0){
          this.setData({
            contentlistdata:this.data.contentlistdata.concat(res.data.baseData.list),
          })
        }
        if(res.data.baseData.lastPage){
          this.setData({
            isEnd:res.data.baseData.lastPage
          })
        }
        this.setData({
          isloading:false
        })
      })
    }else{
      Controller.authorList(data).then(res=>{
        if(res.data.baseData && res.data.baseData.list.length>0){
          this.setData({
            contentlistdata:this.data.contentlistdata.concat(res.data.baseData.list),
          })
        }
        if(res.data.baseData.lastPage){
          this.setData({
            isEnd:res.data.baseData.lastPage
          })
        }
        this.setData({
          isloading:false
        })
      })
    }

    

    
    


  },
  onReachBottom(){
    if(this.data.isEnd){
      return
    }else{
      this.setData({
        page:this.data.page+1
      })
      this.contentlist()
    }
    
  },
  searchSubmit(e){
    console.log(e)
    let _SearchValue = this.data.inputValue;
    let localStorageValue = [];

    if (_SearchValue != '') {

        //添加搜索历史记录
        //调用API从本地缓存中获取数据
        localStorageValue = wx.getStorageSync('searchData') || [];

        let tempSearchData = [];
        //过滤历史列表中相同的搜索记录
        for (let item of localStorageValue) {
            if (item != _SearchValue) {
                tempSearchData.push(item);
            }
        }
        //添加搜索文本进历史记录
        tempSearchData.push(_SearchValue);
        //保存至从本地缓存
        wx.setStorageSync('searchData', tempSearchData);
        
    }
    this.setData({
      page:1,
      pageSize:10,
      isEnd:false,
      contentlistdata:[],
    })
    this.contentlist()
  },
  bindInput(e){
    var value=e.detail.value || ''

      this.setData({
        inputValue: value
      })

  },
  onShareAppMessage: function (e) {
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
})